<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['userName']))
	exit();

function searchABMPalomas()
{
	require("includes/constants.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
		$datatables
			->select('idPaloma, palomaAnillo, palomaName, palomaSexo, palomaColor')
			->from('palomas')
			->where('palomaState <>', $stateErase)
			->join('users', 'users.idUser = palomas.idUser', 'inner')
			->select('userName');
		if($_SESSION['userType'] == $idTypeUser)
			$datatables->where('users.idUser =', $_SESSION['idUser']);
		
	return $datatables->generate();
}

function addOrEdit()
{
	require("includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$DB->StartTrans(); // start transaction
	
	if(!isset($_REQUEST["idPaloma"])){
		$strSQL = "SELECT palomaAnillo FROM palomas WHERE palomaAnillo = '".$_REQUEST["palomaAnillo"]."' AND palomaState <> '$stateErase'";
		$rsPalomas = $DB->Execute($strSQL);		

		if(!$rsPalomas->EOF)
		{
			$arrJSON->status = "ERROR";
			$arrJSON->msg = 'El registro ingresado ya existe';
			return json_encode($arrJSON);
		}
			
		$strSQL = "INSERT INTO palomas ( ";
		$strSQL .= "	palomaAnillo, ";
		$strSQL .= "	palomaName, ";
		$strSQL .= "	palomaState, ";
		$strSQL .= "	palomaAnno, ";
		$strSQL .= "	palomaSexo, ";
		$strSQL .= "	palomaColor, ";
		$strSQL .= "	idUser ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["palomaAnillo"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["palomaName"], "UTF-8")."', ";
		$strSQL .= "	'".$stateAdd."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["palomaAnno"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["optSexo"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["palomaColor"], "UTF-8")."', ";
		if($_SESSION['userType'] == $idTypeUser){
			$strSQL .= "	".$_SESSION['idUser'];
		}
		else {
			$strSQL .= "	".$_REQUEST["palomaUsuario"];
		}
		$strSQL .= "	)";
	}
	else{
		$strSQL = "UPDATE palomas SET ";
		$strSQL .= "	palomaAnillo = '".mb_strtoupper($_REQUEST["palomaAnillo"], "UTF-8")."', ";
		$strSQL .= "	palomaName = '".mb_strtoupper($_REQUEST["palomaName"], "UTF-8")."', ";
		$strSQL .= "	palomaAnno = '".mb_strtoupper($_REQUEST["palomaAnno"], "UTF-8")."', ";
		$strSQL .= "	palomaSexo = '".mb_strtoupper($_REQUEST["optSexo"], "UTF-8")."', ";
		$strSQL .= "	palomaColor = '".mb_strtoupper($_REQUEST["palomaColor"], "UTF-8")."', ";
		if($_SESSION['userType'] == $idTypeAdministrator){
			$strSQL .= "	idUser = ".$_REQUEST["palomaUsuario"].", ";
		}
		$strSQL .= "	palomaState = '".$stateModify."'";
		$strSQL .= " WHERE idPaloma = ". $_REQUEST["idPaloma"];
	}
	
	$DB->Execute($strSQL);
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = 'Ocurri&oacute; un error al grabar el archivo';
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = 'El registro se guard&oacute; correctamente';
	}	
	return json_encode($arrJSON);
}

function del()
{
	require("includes/constants.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	$DB->Execute("UPDATE palomas SET palomaState='$stateErase' where idPaloma=" . $_REQUEST["idPaloma"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = 'Ocurri&oacute; un error al eliminar';
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = 'El registro se elimin&oacute; correctamente';
	}	
	return json_encode($arrJSON);
}

function getUsuarios()
{
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsUsers = $DB->Execute("SELECT idUser, userCompleteName FROM users");
	
	$i=0;
	while(!$rsUsers->EOF){
		$arrUsers->data[$i]['idUser'] = $rsUsers->fields(0);
		$arrUsers->data[$i++]['userCompleteName'] = $rsUsers->fields(1);
		$rsUsers->MoveNext();
	}
	
	return json_encode($arrUsers->data);
}

function editPaloma()
{
	require("includes/constants.php");
		
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsPalomas = $DB->Execute("SELECT * FROM palomas WHERE palomaState <> '$stateErase' and idPaloma=" . $_REQUEST["idPaloma"]); // execute query
						
	if(!$rsPalomas->EOF){
		$joPaloma->data['palomaName'] = $rsPalomas->fields("palomaName");
		$joPaloma->data['palomaAnillo'] = $rsPalomas->fields("palomaAnillo");
		$joPaloma->data['palomaAnno'] = $rsPalomas->fields("palomaAnno");
		$joPaloma->data['palomaColor'] = $rsPalomas->fields("palomaColor");
		$joPaloma->data['palomaSexo'] = $rsPalomas->fields("palomaSexo");
		$joPaloma->data['idUser'] = $rsPalomas->fields("idUser");
		
		return json_encode($joPaloma->data);
	}
	else
		return json_encode('');
}

switch($_REQUEST['actionOfForm'])
{
	case "EDIT":
		echo editPaloma();
		break;
	case "DELETE":
		echo del();
		break;
	case "search":
		echo searchABMPalomas();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;
	case "getUsuarios":
		echo getUsuarios();
		break;
}
?>
